// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: UFO Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 71 : TYPE_UFO
private alias 6 : UFO.frames

// Function declarations
reserve function MobileUFO
reserve function ProtoUFO
// Static Values

// Tables

private table UFOpositions
	32, 1, 2, 32, 1, 16, 2, 16, 2, 32, 1, 2, 1, 32, 2, 16, 2, 16, 1, 2, 32, 1, 32, 1, 2, 64, 1, 2, 1, 32, 2, 16
end table

event ObjectMain
	object.frame = object.animationTimer
	object.frame >>= 4
	temp1 = UFO.frames
	temp1 *= 16
	object.animationTimer++
	object.animationTimer %= temp1
end event


event ObjectDraw
	if screen.yoffset < 640
		switch saved.UFO
			case 1
				CallFunction(MobileUFO)
				break
			case 2
				CallFunction(ProtoUFO)
				break
		end switch
	end if
end event

function ProtoUFO
	//Starting positions
	temp0 = screen.xoffset
	temp0 >>= 3
	FlipSign(temp0)	
	
	temp1 = tileLayer[1].scrollPos
	temp1 /= -0x10000
	temp1 += 10
	temp2 = temp1
	temp2 += 32
	
	temp3 = tileLayer[0].xsize
	temp3 <<= 7
	
	temp4 = 0
	
	while temp0 < temp3
		GetTableValue(temp5, temp4, UFOpositions)
		switch temp5
		case 1
			DrawSpriteScreenXY(object.frame, temp0, temp1)
			temp0 += 32
			break
		case 2
			DrawSpriteScreenXY(object.frame, temp0, temp2)
			temp0 += 32
			break
		default
			temp0 += temp5
			break
		end switch		
		temp4++
		temp4 &= 31
	loop
end function

function MobileUFO
	temp0 = screen.xoffset
	temp0 >>= 3
	temp0 &= 127
	FlipSign(temp0)
	temp1 = tileLayer[1].scrollPos
	temp1 /= -0x10000
	temp1 += 32
	temp2 = screen.xsize
	temp2 += 32
	temp5 = oscillation
	temp5 <<= 1
	while temp0 < temp2
		temp3 = temp0
		temp3 += temp5
		Sin(temp4, temp3)
		temp4 >>= 5
		temp4 += temp1
		Cos(object.alpha, temp3)
		object.alpha >>= 2
		object.alpha += 128
		DrawSpriteScreenFX(object.frame, FX_INK, temp0, temp4)
		temp0 += 64
	loop
	temp0 = screen.xoffset
	temp0 >>= 4
	temp0 &= 127
	temp0 += 32
	FlipSign(temp0)
	while temp0 < temp2
		temp3 = temp0
		temp3 += temp5
		Sin(temp4, temp3)
		temp4 >>= 5
		FlipSign(temp4)
		temp4 += temp1
		Cos(object.alpha, temp3)
		object.alpha >>= 2
		FlipSign(object.alpha)
		object.alpha += 128
		DrawSpriteScreenFX(object.frame, FX_INK, temp0, temp4)
		temp0 += 64
	loop
end function


event ObjectStartup
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(0, -11, 32, 21, 196, 355)
	SpriteFrame(0, -8, 32, 16, 229, 355)
	SpriteFrame(0, -8, 32, 16, 262, 363)
	SpriteFrame(0, -8, 32, 16, 156, 304)
	SpriteFrame(0, -10, 32, 21, 163, 355)	
	SpriteFrame(0, -15, 32, 30, 110, 146)
	
	if saved.UFO > 0
		ResetObjectEntity(11, TypeName[UFO], 0, 0, 0)
		object[11].priority = PRIORITY_ACTIVE
		object[11].drawOrder = 1
		object[11].inkEffect = INK_ALPHA
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
